﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;
using SharepointSolutionInstaller.Views;
using System.Data.SqlClient;
using SharepointSolutionInstaller.Utils.Log;
using SharepointSolutionInstaller.Resources;

namespace SharepointSolutionInstaller.Utils.SolutionDeployment
{
    public abstract class SiteCollectionFeatureCommand : Command
    {
        internal SiteCollectionFeatureCommand(InstallProcess parent) : base(parent) { }
           // Modif JPI - Début
        protected static void DeactivateFeature(IList<SPSite> siteCollections, List<Guid?> featureIds)
        {
            try
            {
                if (siteCollections != null && featureIds != null && featureIds.Count > 0)
                {

                    log.Info(CommonUIStrings.logFeatureDeactivate);
                    foreach (SPSite siteCollection in siteCollections)
                    {
                        foreach (Guid? featureId in featureIds)
                        {
                            if (featureId == null) continue;

                            SPFeature feature = siteCollection.Features[featureId.Value];
                            if (feature == null) continue;

                            siteCollection.Features.Remove(featureId.Value);
                                        log.Info(siteCollection.Url + " : " + featureId.Value.ToString());
                        }
                    }
                }
            }

            catch (ArgumentException ex)  // Missing assembly in GAC
            {
                      log.Warn(ex.Message, ex);
            }

            catch (InvalidOperationException ex)  // Missing receiver class
            {
                          log.Warn(ex.Message, ex);
            }

            catch (SqlException ex)
            {
                throw new Exception(ex.Message, ex);
            }
        }
        // Modif JPI - Fin
    }
}
